const nurl = require('url');
const { BrowserWindow } = require('electron');


function parseInvokeProtocol(url, fromProtocol) {
    const cURL = nurl.parse(url, true);
    return JSON.stringify({
        token: cURL.host,
        ...cURL.query,
        fromProtocol: fromProtocol,
    });
}

function openByUrl(url) {
    let mainWindow = global.mainWindow;
    if (!mainWindow) {
        const windows = BrowserWindow.getAllWindows();
        mainWindow = windows && windows.length > 0 && windows[0];
    }

    mainWindow && mainWindow.webContents.send('protocol-client-url', url);
}

module.exports = {
    parseInvokeProtocol,
    openByUrl
};